package com.lx.config;

import com.lx.service.impl.WebReactiveUserDetailsServiceImpl;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.server.SecurityWebFilterChain;

import lombok.extern.slf4j.Slf4j;

/**
 * 权限配置
 *
 * @author 段誉
 * @create 2019-05-21 9:03
 */
@Slf4j
@Configuration
@EnableWebFluxSecurity
public class WebSecurityConfig {

  @Bean
  public WebReactiveUserDetailsServiceImpl userDetailsService() {
    return new WebReactiveUserDetailsServiceImpl();
  }

  @Bean
  public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    http.authorizeExchange()
            .anyExchange().authenticated()
            .and()
        .httpBasic()
            .and()
        .formLogin();
    return http.build();

  }
  /**
   * 密码加密
   * @return
   */
  @Bean
  public BCryptPasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(4);
  }
}
